//
//  LGTeamDetailViewController.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/7/11.
//  团队详情

#import "LGTeamDetailViewController.h"
//编辑团队信息
#import "LGTeamMessageController.h"
#import "LGTeamDetailHeader.h"
#import "LGTeamMemberCell.h"
@interface LGTeamDetailViewController ()<UITableViewDelegate,UITableViewDataSource,LGBottomButtonDelegate,LGTeamMemberCellDelegate>
/**   */
@property (nonatomic , strong)CAGradientLayer *gradientLayer;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)LGTeamDetailHeader *headView;
/**   */
@property (nonatomic , strong)LGBottomButtonView *bottomView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , assign)BOOL isManager;

@end

@implementation LGTeamDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"团队详情";
    self.actionTitle = (_actionTitle && _actionTitle.length>0)?self.actionTitle:@"";
    [self.view.layer addSublayer:self.gradientLayer];
    self.view.backgroundColor = [UIColor colorWithString:@"#F8F8F8"];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    self.view.backgroundColor = [UIColor colorWithString:@"#F8F8F8"];
    [self emptyViewWithSuperView:self.tableView frame:CGRectMake(0, viewPix(175), Screen_W, self.tableView.frame.size.height-viewPix(175))];
    self.emptyView.backgroundColor = [UIColor clearColor];
    [self requestMemberData];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withGET:@"/team/info" parameters:@{@"id":self.teamid} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.isManager = [returnData[@"manager"] integerValue]>0?YES:NO;
            self.headView.isManager = self.isManager;
            self.headView.dataDic = returnData;
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)requestMemberData{
    [RequestUtil withGET:@"/team/listByTeam" parameters:@{@"teamId":self.teamid} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
//        self.bottomView.hidden = NO;
        if (code == 200) {
            self.dataArray = returnData;
            [self.tableView reloadData];
        }
        self.headView.count = self.dataArray.count;
        if (self.dataArray.count == 0) {
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无团队成员" offset:-50];
//            self.bottomView.hidden = YES;
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (self.dataArray.count == 0) {
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无团队成员" offset:-50];
//            self.bottomView.hidden = YES;
        }
    }];
}

//编辑团队
-(void)editTeamMessage{
    LGTeamMessageController *controller = [[LGTeamMessageController alloc]init];
    controller.teamid = self.teamid;
    controller.canEdit = YES;
    [self.navigationController pushViewController:controller animated:YES];
}
//查看详情
-(void)checkTeamMessage{
    LGTeamMessageController *controller = [[LGTeamMessageController alloc]init];
    controller.teamid = self.teamid;
    controller.canEdit = self.isManager;
    [self.navigationController pushViewController:controller animated:YES];
}

//移除成员
-(void)removeMember:(NSString *)patientid{
    __block NSString *userid = patientid;
    __weak typeof(self) weakSelf = self;
    [LGAlertTool alertWithTitle:@"提示" content:@"是否将该成员移除团队？" sureAction:^{
        [RequestUtil bodayPOST:@"/team/removeTeamUser" parameters:@{@"teamId":weakSelf.teamid,@"userId":userid} header:YES success:^(NSInteger code, NSString *message, id returnData) {
            if (code == 200) {
                [weakSelf requestMemberData];
            }
        } error:^(NSError *error) {
            
        }];
    }];
}

-(void)bottomButtonTouched{
    if ([self.actionTitle isEqualToString:@"申请加入"]) {
        //申请加入
        [RequestUtil bodayPOST:@"/team/interApprove" parameters:@{@"teamId":self.teamid,@"userId":kUserId} header:YES success:^(NSInteger code, NSString *message, id returnData) {
            if (code == 200) {
                [TooltipView showMessage:@"已申请，请等待审批" offset:0];
                [self.navigationController popToViewController:self.backVC animated:YES];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            
        }];
        
    }else if ([self.actionTitle isEqualToString:@"退出团队"]){
        //退出团队
        [RequestUtil bodayPOST:@"/team/exitTeam" parameters:@{@"teamId":self.teamid,@"userId":kUserId} header:YES success:^(NSInteger code, NSString *message, id returnData) {
            if (code == 200) {
                [TooltipView showMessage:@"退出成功" offset:0];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            
        }];
    }else{
        //解散团队
        [RequestUtil bodayPOST:@"/team/dissolve" parameters:@{@"teamId":self.teamid} header:YES success:^(NSInteger code, NSString *message, id returnData) {
            if (code == 200) {
                [TooltipView showMessage:@"解散成功" offset:0];
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            
        }];
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGTeamMemberCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.canRemove = self.isManager;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.delegate = self;
    return cell;
}

//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    LGTeamDetailViewController *controller = [[LGTeamDetailViewController alloc]init];
//    controller.teamid = LGNSString(self.dataArray[indexPath.row][@"id"]);
//    [self.navigationController pushViewController:controller animated:YES];
//}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    return headView;
}
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(65)-bottomSafeBarHeight/4.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGTeamMemberCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headView;
    }
    return _tableView;
}

-(LGTeamDetailHeader *)headView{
    if(!_headView){
        _headView = [[LGTeamDetailHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(170))];
        [_headView.detailBtn addTarget:self action:@selector(editTeamMessage) forControlEvents:UIControlEventTouchUpInside];
        [_headView.checkBtn addTarget:self action:@selector(checkTeamMessage) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headView;
}

-(LGBottomButtonView *)bottomView{
    if(!_bottomView){
        _bottomView = [[LGBottomButtonView alloc] initWithTitle:self.actionTitle color:kThemeColor type:LGBottomButtonTypeBack frame:CGRectMake(0, kMaxY(self.tableView.frame), Screen_W, viewPix(65)+bottomSafeBarHeight/4.0)];
        [_bottomView setShadoWithColor:@"#EFEFEF" offset:CGSizeMake(0, 2) opacity:1 radius:0];
        _bottomView.delegate = self;
    }
    return _bottomView;
}

-(CAGradientLayer *)gradientLayer{
    if(!_gradientLayer){
        _gradientLayer = [CAGradientLayer layer];
        _gradientLayer.frame = CGRectMake(0, 0, Screen_W, viewPix(322));
        _gradientLayer.startPoint = CGPointMake(0.5, 0);
        _gradientLayer.endPoint = CGPointMake(0.5, 1.03);
        _gradientLayer.colors = @[(__bridge id)[UIColor colorWithString:@"#BCB5F5"].CGColor, (__bridge id)[UIColor colorWithString:@"#F8F8F8"].CGColor, (__bridge id)[UIColor colorWithString:@"#F8F8F8"].CGColor];
        _gradientLayer.locations = @[@(0), @(0.5f), @(1.0f)];
    }
    return _gradientLayer;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
